package com.esyoga.yoyoga.service.impl;

import com.esyoga.yoyoga.mapper.TGymAgreementMapper;
import com.esyoga.yoyoga.mapper.TGymMapper;
import com.esyoga.yoyoga.mapper.TTrainerAgreementMapper;
import com.esyoga.yoyoga.mapper.TTrainerMapper;
import com.esyoga.yoyoga.pojo.DTO.TtrainerAgreementDTO;
import com.esyoga.yoyoga.pojo.*;
import com.esyoga.yoyoga.service.AgreementService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("agreementService")
public class AgreementServiceImpl implements AgreementService {

    @Resource
    TTrainerAgreementMapper trainerAgreementMapper;
    @Resource
    TTrainerAgreementExample trainerAgreementExample;
    @Resource
    TGymAgreementMapper gymAgreementMapper;
    @Resource
    TGymAgreementExample gymAgreementExample;
    @Resource
    TGymMapper tGymMapper;
    @Resource
    TTrainerMapper tTrainerMapper;


    @Override
    public List<TTrainerAgreement> selectByGIdAndTId(Integer gId, Integer tId) {
        trainerAgreementExample.clear();
        TTrainerAgreementExample.Criteria criteria = trainerAgreementExample.createCriteria();
        criteria.andTaGIdEqualTo(gId).andTaTIdEqualTo(tId).andTaStateEqualTo(4);
        return trainerAgreementMapper.selectByExample(trainerAgreementExample);

    }

    @Override
    public List<TGymAgreement> selectByGId(Integer gId) {
        gymAgreementExample.clear();
        TGymAgreementExample.Criteria criteria = gymAgreementExample.createCriteria();
        criteria.andGaGIdEqualTo(gId).andGaStateEqualTo(2);
        return gymAgreementMapper.selectByExample(gymAgreementExample);
    }

    @Override
    public List<TTrainerAgreement> selectAgreementBytIdAndaState(Integer tId, int aState) {
        trainerAgreementExample.clear();
        TTrainerAgreementExample.Criteria criteria = trainerAgreementExample.createCriteria();
        criteria.andTaTIdEqualTo(tId).andTaStateEqualTo(aState);
        return trainerAgreementMapper.selectByExample(trainerAgreementExample);
    }

    /**
     * 根据trainerAgreementList查找trainerAgreementDTOList
     * @param trainerAgreementList
     * @return
     */
    @Override
    public List<TtrainerAgreementDTO> selectTrainerAgreementDTO(List<TTrainerAgreement> trainerAgreementList) {
        List<TtrainerAgreementDTO> trainerAgreementDTOList = new ArrayList<>();

        for(TTrainerAgreement trainerAgreement : trainerAgreementList){
            TGym gym = tGymMapper.selectByPrimaryKey(trainerAgreement.getTaGId());
            TTrainer trainer = tTrainerMapper.selectByPrimaryKey(trainerAgreement.getTaTId());
            TtrainerAgreementDTO trainerAgreementDTO = new TtrainerAgreementDTO(gym, trainer,trainerAgreement);
            trainerAgreementDTOList.add(trainerAgreementDTO);
        }
        return trainerAgreementDTOList;
    }

    /**
     * 插入场馆和教练之间签订的合约
     * @param trainerAgreement
     * @return
     */
    @Override
    public int insertTrainerAgreement(TTrainerAgreement trainerAgreement){
        return trainerAgreementMapper.insertSelective(trainerAgreement);
    }

    @Override
    public List<TTrainerAgreement> selectByGIdAndTIdOnGoing(Integer gId, Integer tId, List<Integer> state) {
        trainerAgreementExample.clear();
        TTrainerAgreementExample.Criteria criteria = trainerAgreementExample.createCriteria();
        criteria.andTaGIdEqualTo(gId).andTaTIdEqualTo(tId).andTaStateIn(state);
        return trainerAgreementMapper.selectByExample(trainerAgreementExample);
    }
}
